/**
 * 
 */
package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import domain.Laptop;
import domain.Server;

/**
 * @author grupo22
 *
 */
public class JDBCServerDAO implements IServerDAO {

	/* (non-Javadoc)
	 * @see data.IServerDAO#select(java.sql.Connection, java.lang.String, domain.Server)
	 */
	@Override
	public Server select(Connection con, String oid, Server s) {
		String sql = "SELECT * FROM Server WHERE (OIDProduct = ? )";
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);

            rs = stmt.executeQuery();

            rs.next();
            s.setProcessor(rs.getString("processor"));
            s.setMemory(rs.getInt("memory"));
            s.setDiscsNumber(rs.getInt("discsNumber"));
    		s.setDebianComp(rs.getBoolean("debianComp"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
        return s;
	}

	@Override
	public void insert(Connection con, Server s, String oid) {
		String sql = "INSERT INTO Server (OIDProduct, processor, memory, discsNumber, debianComp) VALUES (?,?,?,?,?)";
        PreparedStatement stmt = null;

        try {
            stmt = con.prepareStatement(sql);
            stmt.setString(1, oid);
            stmt.setString(2, s.getProcessor());
            stmt.setInt(3, s.getMemory());
            stmt.setInt(4, s.getDiscsNumber());
            stmt.setBoolean(5, s.isDebianComp());

            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) { }
        }
	}
}
